package vip.liux.front.infrastructure.constants;

import com.google.common.util.concurrent.RateLimiter;

public class RateLimiterConsts {

    /**
     * 账号注册限流器 每秒只发出5个令牌
     */
    private final static RateLimiter accountRegisterRateLimiter = RateLimiter.create(5.0);

    /**
     * 短信发送限流器    每秒只发出3个令牌
     */
    private final static RateLimiter smsSendRateLimiter = RateLimiter.create(3.0);

    /**
     * 尝试获取令牌，返回尝试结果
     *
     * @return true:获取成功 false:获取失败
     */
    public static boolean tryAccountRegisterAcquire() {
        return accountRegisterRateLimiter.tryAcquire();
    }

    /**
     * 取令牌，暂时取不到会一直去尝试
     *
     * @return 返回获取令牌的时间
     */
    public static double accountRegisterAcquire() {
        return accountRegisterRateLimiter.acquire();
    }

    /**
     * 尝试获取令牌
     *
     * @return true:获取成功 false:获取失败
     */
    public static boolean trySmsSendAcquire() {
        return smsSendRateLimiter.tryAcquire();
    }
}
